﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblAccessRightDAL:ItblAccessRight
    {
        HKEA_CMSEntities model;
        tblAccessRight right;

        public tblAccessRightDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblAccessRightDAL GetInstance()
        {
            return new tblAccessRightDAL();
        }

        public int InsertENT(tblAccessRight ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblAccessRights.AddObject(ent);
            return model.SaveChanges();
        }

        public List<tblAccessRight> GetListByENT(tblAccessRight ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblAccessRights.Where(a => a.AccessRightUID > 0);
            if (ent.AccessRightUID > 0)
                result = result.Where(a => a.AccessRightUID == ent.AccessRightUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<tblAccessRight> GetListByENT(tblAccessRight ent)
        {
            var result = model.tblAccessRights.Where(a => a.AccessRightUID > 0);
            if (ent.AccessRightUID > 0)
                result = result.Where(a => a.AccessRightUID == ent.AccessRightUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(tblAccessRight ent)
        {
            var result = model.tblAccessRights.Where(a => a.AccessRightUID > 0);
            if (ent.AccessRightUID > 0)
                result = result.Where(a => a.AccessRightUID == ent.AccessRightUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblAccessRight ent)
        {
            right = model.tblAccessRights.Where(a=>a.AccessRightUID==ent.AccessRightUID).FirstOrDefault();
            if (right != null)
            {
                right.LastModifyBy = ent.LastModifyBy;
                right.LastModifyDate = DateTime.Now;
                right.FunctionUID = ent.FunctionUID;
                right.PrivilegeUID = ent.PrivilegeUID;
                right.UserID = ent.UserID;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblAccessRight ent)
        {
            var temp = model.tblAccessRights.Where(a => a.AccessRightUID > 0);
            if (ent.AccessRightUID > 0)
                temp = temp.Where(a=>a.AccessRightUID==ent.AccessRightUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblAccessRights.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
